package com.es;

import com.es.model.Student;
import com.es.service.StudentServiceI;
import com.es.service.impl.StudentServiceImpl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class SIM {

    /**
     * 日期格式定义
     */
    private static String dataPattern = "yyyy-MM-dd";

    /**
     * 定义的一个扫描器
     */
    private static Scanner scanner = new Scanner(System.in);

    /**
     * studentService对象
     */
    private static StudentServiceI service = new StudentServiceImpl();

    public static void main(String[] args) {
       while(true){
           showMenu();
           System.out.print("请选择您要使用的功能：");
           int op = scanner.nextInt();
           switch (op){
               case 1: save(); break;
               case 2: list(); break;
               case 3: get(); break;
               case 4: update(); break;
               case 5: delete(); break;
               case 6: getByPhone(); break;
               case 7: listByName(); break;
               default:;
               case 8: System.exit(0);

           }
       }
    }

    private static void listByName() {
        System.out.print("输入姓名：");
        String name = scanner.next();
        List<Student> list = service.listByName(name);
        showStudentInfo(list);
    }


    private static void getByPhone() {
        System.out.print("输入手机号：");
        String phone = scanner.next();
        Student s = service.get(phone);
        showStudentInfo(s);

    }

    private static void delete() {
        System.out.print("输入要删除的学号：");
        int id = scanner.nextInt();
        int i = service.delete(id);
        if(i > 0){
            System.out.println("删除成功");
        }else{
            System.out.println("删除失败");
        }
    }

    private static void update() {
        Student student = new Student();

        System.out.print("请输入学号：");
        int id = scanner.nextInt();
        student.setId(id);
        //int id = s
        System.out.print("请输入姓名：");
        String name = scanner.next();
        student.setName(name);

        System.out.print("请输入生日（YYYY-MM-DD）：");
        String str = scanner.next();
        Date date = parse(str);
        student.setBirthday(date);

        System.out.print("请输入电话：");
        String phone = scanner.next();
        student.setPhone(phone);

        System.out.print("请输入地址：");
        String address = scanner.next();
        student.setAddress(address);

        int i = service.update(student);
        if(i > 0){
            System.out.println("修改成功");
        }else{
            System.out.println("修改失败");
        }
    }

    private static void get() {
        System.out.print("输入您要查看相信信息的学生学号：");
        int id = scanner.nextInt();
        Student s = service.get(id);
        showStudentInfo(s);
    }


    private static void list() {
        List<Student> list = service.list();
        System.out.println("学号\t姓名\t生日\t联系电话\t联系地址");
        showStudentInfo(list);
    }

    private static void save() {
        Student student = new Student();
        //int id = s
        System.out.print("请输入姓名：");
        String name = scanner.next();
        student.setName(name);

        System.out.print("请输入生日（YYYY-MM-DD）：");
        String str = scanner.next();
        Date date = parse(str);
        student.setBirthday(date);

        System.out.print("请输入电话：");
        String phone = scanner.next();
        student.setPhone(phone);

        System.out.print("请输入地址：");
        String address = scanner.next();
        student.setAddress(address);

        int i = service.save(student);
        if(i > 0){
            System.out.println("保存成功");
        }else{
            System.out.println("保存失败");
        }
    }

    private static void showMenu() {


        System.out.println("---------------------------------");
        System.out.println("     欢迎使用学生信息管理系统        ");
        System.out.println("-----1. 添加                   ---");
        System.out.println("-----2.查看所有                 ---");
        System.out.println("-----3.查看详细                 ---");
        System.out.println("-----4.修改学生信息              ---");
        System.out.println("-----5.删除学生信息              ---");
        System.out.println("-----6.电话搜索学生信息           ---");
        System.out.println("-----7.姓名搜索学生信息           ---");
        System.out.println("-----8.退出系统                  ---");
        System.out.println("-----------------------------------");

    }


    private static Date parse(String str){
        SimpleDateFormat dateFormat = new SimpleDateFormat(dataPattern);
        try {
            return dateFormat.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new Date();
    }

    private static String format(Date date){
        SimpleDateFormat dateFormat = new SimpleDateFormat(dataPattern);
        return dateFormat.format(date);
    }

    private static void showStudentInfo(List<Student> list) {
        System.out.println("学号\t姓名\t生日\t联系电话\t联系地址");
        if(null==list || list.isEmpty()){
            System.out.println("暂无数据");
        }
        list.forEach(s->{
            System.out.println(s.getId()+"\t"+s.getName()+"\t"+format(s.getBirthday())+"\t"+s.getPhone()+"\t"+s.getAddress());
        });
    }


    private static void showStudentInfo(Student s) {
        System.out.println("学号\t姓名\t生日\t联系电话\t联系地址");
        if(s == null){
            System.out.println("查询失败");
        }else{
            System.out.println(s.getId()+"\t"+s.getName()+"\t"+format(s.getBirthday())+"\t"+s.getPhone()+"\t"+s.getAddress());
        }
    }



}
